package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;

import com.igexin.sdk.PushManager;
import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.config.UserInfo;
import com.nest.WorkNest.tools.FileHelper;
import com.nest.WorkNest.tools.Tool;

public class WelcomeAct extends BaseActivity {
    private FileHelper openTimes;
    private Context context = WelcomeAct.this;
    private String TIMES = "opentimes";
    private boolean first;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        hideStatusBar();
        setContentView(R.layout.act_welcome);
        openTimes = new FileHelper(getBaseContext(), TIMES);
        PushManager.getInstance().initialize(this.getApplicationContext());
        first = openTimes.getBoolean("first", true);
        if (first) {
            turn2Navigation();
        } else {
            skipNavigation();
        }

    }

    public void turn2Navigation() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(context, NavigationAct.class);
                startActivity(intent);
                finish();
            }
        }, 2000);
    }

    public void skipNavigation() {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                if (fileHelper.getBoolean(UserInfo.USERINFO_ISLOGIN)) {
                    loadPerson();
                } else {
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }
            }
        }, 2000);
    }

    private void hideStatusBar() {
        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(attrs);
    }

    protected void onStop() {
        super.onStop();
        // We need an Editor object to make preference changes.
        // All objects are from android.context.Context
        if (first) {
            openTimes.putBoolean("first", false);
        }
        // Commit the edits!
    }


    public void loadPerson() {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                if (Tool.getString(result, "resultCode").equals("0")) {
                    Intent intent = new Intent();
                    intent.setClass(context, MainTabAct.class);
                    startActivity(intent);
                    finish();
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostUserDetail(context, ioAuthCallBack);
    }
}
